package com.punai.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.punai.common.core.domain.entity.SysPnDictionary;
import com.punai.common.utils.PnDictUtil;
import com.punai.system.constants.SysLogInfoConstants;
import com.punai.system.domain.SysLogInfo;
import com.punai.system.mapper.SysLogInfoMapper;
import com.punai.system.service.SysLogInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class SysLogInfoServiceImpl implements SysLogInfoService {
    @Autowired
    private SysLogInfoMapper sysLogInfoMapper;

    @Override
    public List<SysLogInfo> list(SysLogInfo sysLogInfo,List<String> types) {
        return sysLogInfoMapper.selectSysLogInfoList(sysLogInfo,types);
    }

    @Override
    public SysLogInfo saveSysLogInfo(String content, String uri, String type, String message) {
        Collection<SysPnDictionary> interfaceDesc = PnDictUtil.getDictionarys("066");
        interfaceDesc.addAll(PnDictUtil.getDictionarys("067"));
        String decs = null;
        if (CollUtil.isNotEmpty(interfaceDesc)){
            for (SysPnDictionary dictionary : interfaceDesc) {
                String value = dictionary.getValue();
                if (uri.contains(value)){
                    decs = dictionary.getName();
                }
            }
        }
        SysLogInfo logDataInfo = new SysLogInfo();
        String[] desc = SysLogInfoConstants.LogType.getDescByCode(type).split("-", -1);
        if (desc.length > 0){
            logDataInfo.setPush(desc[0]);
            logDataInfo.setReceive(desc[1]);
        }
        logDataInfo.setType(message.contains("\"code\":1") ? "0" : "1");
        logDataInfo.setPushTime(new Date());
        logDataInfo.setContent(content);
        logDataInfo.setInterfacename(decs);
        logDataInfo.setMessage(message);
        logDataInfo.setLogType(type);
        sysLogInfoMapper.insert(logDataInfo);
        return logDataInfo;
    }

    @Override
    public SysLogInfo saveSysLogInfo(String content, String uri, String type, String message, String proCode, String proName) {
        Collection<SysPnDictionary> interfaceDesc = PnDictUtil.getDictionarys("066");
        interfaceDesc.addAll(PnDictUtil.getDictionarys("067"));
        String decs = null;
        if (CollUtil.isNotEmpty(interfaceDesc)){
            for (SysPnDictionary dictionary : interfaceDesc) {
                String value = dictionary.getValue();
                if (uri.contains(value)){
                    decs = dictionary.getName();
                }
            }
        }
        SysLogInfo logDataInfo = new SysLogInfo();
        String[] desc = SysLogInfoConstants.LogType.getDescByCode(type).split("-", -1);
        if (desc.length > 0){
            logDataInfo.setPush(desc[0]);
            logDataInfo.setReceive(desc[1]);
        }
        logDataInfo.setType(message.contains("\"code\":1") ? "0" : "1");
        logDataInfo.setPushTime(new Date());
        logDataInfo.setContent(content);
        logDataInfo.setInterfacename(decs);
        logDataInfo.setMessage(message);
        logDataInfo.setProCode(proCode);
        logDataInfo.setProName(proName);
        logDataInfo.setLogType(type);
        sysLogInfoMapper.insert(logDataInfo);
        return logDataInfo;
    }

    @Override
    public int updateSysLogInfo(SysLogInfo sysLogInfo, String message) {
        if (sysLogInfo == null) return 0;
        sysLogInfo.setType(message.contains("\"code\":1") ? "0" : "1");
        sysLogInfo.setMessage(message);
        return sysLogInfoMapper.updateById(sysLogInfo);
    }
}
